<?php

use yii\db\Migration;

/**
 * Class m250613_094258_add_term_field_to_system_config
 */
class m250613_094258_add_term_field_to_system_config extends Migration
{
    /**
     * {@inheritdoc}
     */
    public function safeUp()
    {
        $this->addColumn('{{%tbl_system_config}}', 'term_type', $this->string(20)->null()->after('config_value')->comment('期限类型：short_term(1-5年)或long_term(5年以上)'));
        $this->addColumn('{{%tbl_system_config}}', 'rate_type', $this->string(20)->null()->after('term_type')->comment('利率类型：commercial(商贷)或fund(公积金)'));

        // 更新现有数据结构
        // 处理商业贷款利率选项
        $this->update('{{%tbl_system_config}}', [
            'config_value' => json_encode([
                ['label' => '3.8%', 'value' => 3.8],
                ['label' => '4.0%', 'value' => 4.0],
                ['label' => '4.3%', 'value' => 4.3],
                ['label' => '4.5%', 'value' => 4.5],
                ['label' => '4.7%', 'value' => 4.7],
                ['label' => '5.0%', 'value' => 5.0]
            ], JSON_UNESCAPED_UNICODE),
            'rate_type' => 'commercial'
        ], ['config_key' => 'commercial_rate_options']);
        
        // 处理公积金贷款利率选项
        $this->update('{{%tbl_system_config}}', [
            'config_value' => json_encode([
                ['label' => '3.1%', 'value' => 3.1],
                ['label' => '3.3%', 'value' => 3.3],
                ['label' => '3.5%', 'value' => 3.5]
            ], JSON_UNESCAPED_UNICODE),
            'rate_type' => 'fund'
        ], ['config_key' => 'fund_rate_options']);
        
        // 处理默认商业贷款利率
        $this->update('{{%tbl_system_config}}', [
            'config_value' => json_encode([
                ['label' => '3.8%', 'value' => 3.8]
            ], JSON_UNESCAPED_UNICODE),
            'rate_type' => 'commercial'
        ], ['config_key' => 'default_commercial_rate']);
        
        // 处理默认公积金贷款利率
        $this->update('{{%tbl_system_config}}', [
            'config_value' => json_encode([
                ['label' => '3.1%', 'value' => 3.1]
            ], JSON_UNESCAPED_UNICODE),
            'rate_type' => 'fund'
        ], ['config_key' => 'default_fund_rate']);
    }

    /**
     * {@inheritdoc}
     */
    public function safeDown()
    {
        $this->dropColumn('{{%tbl_system_config}}', 'term_type');
        $this->dropColumn('{{%tbl_system_config}}', 'rate_type');
        
        return true;
    }

    /*
    // Use up()/down() to run migration code without a transaction.
    public function up()
    {

    }

    public function down()
    {
        echo "m250613_094258_add_term_field_to_system_config cannot be reverted.\n";

        return false;
    }
    */
}
